
<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <!-- <i class="el-icon-search"></i>
        <span>敏感词库</span> -->
        <!-- <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button> -->
      </div>
      <div style="margin-top: 15px;display: flex;align-items: center;justify-content: center;">
        <el-form :inline="true" :model="listQuery"  label-width="140px" style="display: flex;align-items: center;">
          <el-form-item label="搜索敏感词：" style="margin: 0 10px 0 0;">
            <el-input v-model="listQuery.content" class="input-width" placeholder="搜索敏感词" clearable></el-input>
          </el-form-item>
        </el-form>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <el-button size="mini" class="btn-add" @click="handleAdd()">新增敏感词</el-button>
      <span @click="refBtn" style="cursor: pointer;">刷新列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="adminTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column label="敏感词" align="center">
          <template slot-scope="scope">
            <!-- {{scope.row.content}} -->
            <div v-html="hightLight(listQuery.content, scope.row.content)"></div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="480" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="text"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="添加自定义敏感词"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="admin"
               ref="adminForm"
               label-width="150px" size="small">
        <el-form-item label="敏感词：">
          <el-input v-model="admin.content" style="width: 250px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
  import {fetchList,createAdmin,updateAdmin,updateStatus,deleteAdmin,getRoleByAdmin,allocRole} from '@/api/login';
  import {formatDate} from '@/utils/date';
  import { sensitiveList, sensitiveAdd, sensitiveDelete, fetchAllResourceList } from "@/api/app";

  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    content: null
  };
  const defaultAdmin = {
    content: null,
  };
  export default {
    name: 'sensitiveFilter',
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        admin: Object.assign({}, defaultAdmin),
        allocRoleIds:[],
        allocAdminId:null,
        btnMenu: {
          edit: true,
          delete: true,
        }
      }
    },
    created() {
      const menu = JSON.parse(localStorage.getItem('listMenu'));
      const item = menu.filter(item => item.title === '敏感词过滤')[0];
      fetchAllResourceList({menuId: item.id }).then(res => {
        res.data.forEach(dom => {
          if (dom.component === '' && dom.name === '后台角色管理') {
            this.btnMenu.edit = false;
          }
          if (dom.component === '' && dom.name === '后台角色删除') {
            this.btnMenu.delete = false;
          }
        });
      });
      this.getList();
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return '-';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    methods: {
      hightLight (keyWord, suggtion) {
        // 使用 regexp 构造函数，因为这里要匹配的是一个变量
        let newSrt = null;
        const reg = new RegExp(keyWord, 'ig')
        if (keyWord && suggtion) {
            newSrt = String(suggtion).replace(reg, function (p) {
                return '<span style="color: #409EFF">' + p + '</span>'
            })
        } else {
            newSrt = suggtion;
        }
        return newSrt
      },
      refBtn() {
        this.getList();
      },
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
        this.getList();
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.admin = Object.assign({},defaultAdmin);
      },
      handleStatusChange(index, row) {
        this.$confirm('是否要修改该状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatus(row.id, {status: row.status}).then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消修改'
          });
          this.getList();
        });
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该敏感词?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            sensitiveDelete({id: row.id, content: row.content}).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            sensitiveAdd(this.admin).then(response => {
                this.$message({
                message: '添加成功！',
                type: 'success'
                });
                this.dialogVisible =false;
                this.getList();
            })
        })
      },
      getList() {
        this.listLoading = true;
        sensitiveList(this.listQuery).then(response => {
            // console.log(response);
          this.listLoading = false;
          this.list = response.data.records;
          this.total = response.data.total;
        });
      },
      getRoleListByAdmin(adminId) {
        getRoleByAdmin(adminId).then(response => {
          let allocRoleList = response.data;
          this.allocRoleIds=[];
          if(allocRoleList!=null&&allocRoleList.length>0){
            for(let i=0;i<allocRoleList.length;i++){
              this.allocRoleIds.push(allocRoleList[i].id);
            }
          }
        });
      }
    }
  }
</script>
<style></style>


